package com.heatup.apt.api.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.heatup.apt.api.service.UserShareService;
import com.heatup.apt.common.dao.ActivityInfoMapper;
import com.heatup.apt.common.dao.ActivityInfoResultModelMapper;
import com.heatup.apt.common.dao.CouponApiResultMapper;
import com.heatup.apt.common.dao.CouponInfoMapper;
import com.heatup.apt.model.api.ApiResultModel;
import com.heatup.apt.model.api.coupon.CouponApiResultModel;
import com.heatup.apt.model.coupon.ActivityInfo;
import com.heatup.apt.model.coupon.CouponInfo;
@Service
public class UserShareServiceImpl implements UserShareService {
	
	@Autowired
	private ActivityInfoResultModelMapper activityInfoResultModelMapper;
	
	@Autowired
	private ActivityInfoMapper activityInfoMapper;
	
	@Autowired
	private CouponInfoMapper couponInfoMapper;
	
	@Autowired
	private CouponApiResultMapper couponApiResultMapper;
	
	
	/**
	 * 邀请好友获得优惠券
	 */
	@Override
	public Map<String, Object> getMyCoupon(int id,int activity_id) {
		ActivityInfo activityInfo = activityInfoMapper.selectById(activity_id);
		String couponIds = activityInfo.getCouponId();
		int couponId = Integer.valueOf(couponIds);
		List<Map<String,Object>> list = activityInfoResultModelMapper.queryMyInviteListByUserId(id,activity_id);
		Map<String,Object> map  = activityInfoResultModelMapper.queryMyInviteGetCoupon(id,couponId,activity_id);
		if(map != null){
			map.put("list", list);
		}else{
			// 当不存在的时候 这里测试先写死
			CouponInfo couponInfo = couponInfoMapper.selectById(couponId);
			map = new HashMap<String,Object>();
			map.put("list", new ArrayList<Map<String,Object>>());
			map.put("activity_name", activityInfo.getActivityName());
			map.put("activity_id",activityInfo.getId());
			map.put("invite_all_num", 0);
			map.put("coupon_id",activityInfo.getCouponId());
			map.put("invite_num", 0);
			map.put("invite_money", couponInfo.getCashMoney());
			map.put("invite_amount", 0);
			
		}
		
		return map;
	}

	
	/**
	 * 查询 分享链接可获得的优惠券
	 * 1，邀请者 可获得的优惠券
	 * 2，被邀请者可获得的优惠券
	 */
	@Override
	public ApiResultModel queryMyCouponList(Map<String, Object> paramsMap) {
		ApiResultModel model = new ApiResultModel();
		ActivityInfo activityInfo = activityInfoMapper.selectById((Integer) paramsMap.get("activity_id"));
		List<CouponInfo> listC = new ArrayList<CouponInfo>();
		List<Integer> list =  new ArrayList<Integer>();
		String isSelf = (String) paramsMap.get("isSelf");
		Map<String, Object> responseMap = new HashMap<String, Object>();
		String coupon[] = null;
		String coupons = null;//
		// 1 true 邀请者 可获得的优惠券
		if ("true".equals(isSelf)) {
			coupons = activityInfo.getCouponId();
		} else {
			// 2 false 被邀请者可获得的优惠券
			coupons = activityInfo.getInviteCouponId();
		}
		if(coupons.indexOf("#") > 0){
			coupon = coupons.split("#");
			for(String ci:coupon){
				list.add(Integer.valueOf(ci));
			}
		}else{
			list.add(Integer.valueOf(coupons));
		}
		
		// 1 true 邀请者自己点击链接的时候
		if("true".equals(isSelf)){
			Map<String,Object> mapT = new HashMap<>();
			mapT.put("ids",list);
			mapT.put("pageNo",0);
			mapT.put("pageSize",100);
			responseMap.put("data", couponInfoMapper.queryCouponList(mapT));
		}else{
		// 2 false 被邀请者点击链接的时候
			List<CouponApiResultModel> couponApiResultModel = null;//couponApiResultMapper.queryUserCouponListBy(paramsMap);
			paramsMap.put("couponId", activityInfo.getCouponId());
			paramsMap.put("coupons",list);
			couponApiResultModel = couponApiResultMapper.queryUserCouponListBy(paramsMap);
			responseMap.put("data",couponApiResultModel);
		}
		model.setData(responseMap);
		return model;
	}

}
